//
// NLSESolverWindowController.m
// NLSE Solver, Created by jmpp on 9/2/10.
// $Id: NLSESolverWindowController.m 16 2010-11-03 14:19:32Z jmpalacios $

/**
* =================================================================
* Copyright (C) 2010, Juan Manuel Palacios. All Rights Reserved.
*
* This software project, which includes this module, is licensed
* as described in the file LICENSE, which you should have received
* as part of this distribution, and may be used only according to
* the terms described therein.
*
* =================================================================
**/


#import "NLSESolverWindowController.h"
#import "NLSESolverView.h"


@implementation NLSESolverWindowController

@synthesize inputFields, inputProfile, sSEEnabler, sSEParameter, rEEnabler, rEParameter;
@synthesize iTDView, iFDView, pTDView, pFDView;
@synthesize progressBar;
@synthesize libInput;


- (id) init
{
 
    self = [super initWithWindowNibName:MAIN_WINDOW_NIB_NAME
                                        owner:self];
    return self;
    
}


- (void) windowDidLoad
{

    [iTDView setViewController:self];
    [iFDView setViewController:self];
    [pTDView setViewController:self];
    [pFDView setViewController:self];
    return;
    
}


- (IBAction) enableSelfSteepeningEffect:(id)sender
{
        
    NSInteger state = [sender state];
    switch (state)
    {
    case NSOnState:
        [sSEParameter setEnabled:YES];
        break;
    case NSOffState:
        [sSEParameter setEnabled:NO];
        break;
    }
    
    return;

}


- (IBAction) enableRamanEffect:(id)sender
{
 
    NSInteger state = [sender state];
    switch (state)
    {
        case NSOnState:
            [rEParameter setEnabled:YES];
            break;
        case NSOffState:
            [rEParameter setEnabled:NO];
            break;
    }
    
    return;
    
}


- (IBAction) clearAllInputs:(id) __attribute__((unused)) sender
{
    
    // DEBUG!
    NSLog(@"Clearing out all input values...");
    
    return;
    
}


- (IBAction) startSimulation:(id) __attribute__((unused)) sender
{
        
    // 1. read the data from the UI
    // 2. pass it to a library function that validates it, thus kicking off the algorithm.
    // I would also have to initialize the library somehwere, maybe, because it's going to be dependent on other libraries
    // and if they require initialization my library will have to provide an initialization stage of its own where it takes
    // care of its dependencies.
        
// 1. For each floating point value, need to figure out how to abstract reading a float or a double, depending on whether I'm
//  operating in single or in double precision mode.
// 2. For the sSEParameter and rEParameter, I need to figure out how to clear their values (and not take them into any consideration)
// when they are disabled in the GUI.
// 3. the index of the selected item in the input profile pop-up would most probably select a matching (ordered) entry in a
//  function pointers array declared somewhere, don't know where... yet.
// 4. every time I hit enter in the GUI this whole routine is triggered, meaning a new simulaiton is requested, new struct is allocated, etc.
//  Needless to say, this needs addressing, i.e.: a) hitting return by mistake would destroy the graphics displayed, which is bad of course, so address that!
//  b) before legitimately starting a new simulation we have to destroy the previously allocated struct, to avoid leaks, but maybe at the end of this method
// is not the best place to do it... consider the scenario of requesting a simulation, studying it, requesting the same simulation again, requesting a new one, etc.
    
    ssfm_uinteger fft_points = [[inputFields cellAtIndex:kFFTPointsInput] integerValue];
    if (!fft_points)
    {
        NSAlert *alertDialogue = [[NSAlert alloc] init];
        [alertDialogue setAlertStyle:NSWarningAlertStyle];
        [alertDialogue setMessageText:NSLocalizedString(@"UNUSABLE_PARAMETER_MESSAGE_TEXT",@"Unsuable parameter warning message text.")];
        [alertDialogue setInformativeText:NSLocalizedString(@"FFT_POINTS_WARNING",@"Warning about the \"FFT Points\" input parameter being zero.")];
        [alertDialogue beginSheetModalForWindow:[self window]
                                  modalDelegate:nil
                                 didEndSelector:nil
                                    contextInfo:NULL];
        return;
    }
    
    if ( (libInput = ssfm_init(fft_points)) != NULL)
    {        
        // all these readings are currently unused, they need to be placed somewhere inside the libInput struct.
        [inputProfile indexOfSelectedItem];
        [[inputFields cellAtIndex:kTimeWidthInput] doubleValue];
        [[inputFields cellAtIndex:kTimeWindowInput] doubleValue];
        [[inputFields cellAtIndex:kInputPowerInput] doubleValue];
        [[inputFields cellAtIndex:kDistanceInput] doubleValue];
        [[inputFields cellAtIndex:kNumberOfStepsInput] integerValue];
        [[inputFields cellAtIndex:kGammaInput] doubleValue];
        [[inputFields cellAtIndex:kBeta2Input] doubleValue];
        [[inputFields cellAtIndex:kBeta3Input] doubleValue];
        [[inputFields cellAtIndex:kAlphaInput] doubleValue];

        [sSEParameter doubleValue];
        [rEParameter doubleValue];

        [iTDView setNeedsDisplay:YES];
        [iFDView setNeedsDisplay:YES];
        [pTDView setNeedsDisplay:YES];
        [pFDView setNeedsDisplay:YES];
    }
        
    return;
    
}


- (IBAction) clearAllGraphs:(id) __attribute__((unused)) sender
{
    ssfm_destroy(libInput);
    libInput = NULL;
    [iTDView setNeedsDisplay:YES];
    [iFDView setNeedsDisplay:YES];
    [pTDView setNeedsDisplay:YES];
    [pFDView setNeedsDisplay:YES];
    
    return;
    
}


- (void) windowDidResize:(NSNotification *) __attribute__((unused)) theNotification
{
    
    //[19:48]  <  mikeash> just write your code to handle the view being an arbitrary size
    //[19:48]  <  mikeash> and then you're done
    // sending the -object message to the notification gives you the object that originated it.
    return;

}


@end
